<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>用户注册</title>
    <link href="/static/css/layui.css" rel="stylesheet">
</head>

<body class="layui-padding-3">

    <style>
        .demo-reg-container {
            width: 320px;
            margin: 21px auto 0;
            position: absolute;
            top: 40%;
            left: 50%;
            transform: translate(-50%, -50%);
        }

        .demo-reg-other .layui-icon {
            position: relative;
            display: inline-block;
            margin: 0 2px;
            top: 2px;
            font-size: 26px;
        }
    </style>
    <form class="layui-form">
        <div class="demo-reg-container">
            <p style="text-align: center; font-size: 28px; margin: 20px;">注册帐号</p>
            <div class="layui-form-item">
                <div class="layui-input-wrap">
                    <div class="layui-input-prefix">
                        <i class="layui-icon layui-icon-username"></i>
                    </div>
                    <input type="text" name="username" value="" lay-verify="required" placeholder="用户名"
                    lay-reqtext="请填用户名" autocomplete="off" class="layui-input" id="reg-cellphone">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-wrap">
                    <div class="layui-input-prefix">
                        <i class="layui-icon layui-icon-password"></i>
                    </div>
                    <input type="password" name="password" value="" lay-verify="required" placeholder="密码"
                        autocomplete="off" class="layui-input" id="reg-password" lay-affix="eye">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-wrap">
                    <div class="layui-input-prefix">
                        <i class="layui-icon layui-icon-password"></i>
                    </div>
                    <input type="password" name="confirmPassword" value="" lay-verify="required|confirmPassword"
                        placeholder="确认密码" autocomplete="off" class="layui-input" lay-affix="eye">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-wrap">
                    <div class="layui-input-prefix">
                        <i class="layui-icon layui-icon-username"></i>
                    </div>
                    <input type="text" name="name" value="" lay-verify="required" placeholder="姓名"
                        autocomplete="off" class="layui-input" lay-affix="clear">
                </div>
            </div>
            <div class="layui-form-item">
                <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="demo-reg">注册</button>
            </div>
            <div class="layui-form-item demo-reg-other">
                <label>已有帐号</label>
                <a href="/"> 去登录</a>
            </div>
        </div>
    </form>

    <script src="/static/layui.js"></script>
    <script>
        var $
        layui.use(function () {
            $ = layui.$;
            var form = layui.form;
            var layer = layui.layer;
            var util = layui.util;

            // 自定义验证规则
            form.verify({
                // 确认密码
                confirmPassword: function (value, item) {
                    var passwordValue = $('#reg-password').val();
                    if (value !== passwordValue) {
                        return '两次密码输入不一致';
                    }
                }
            });

            // 提交事件
            form.on('submit(demo-reg)', function (data) {
                var field = data.field; // 获取表单字段值


                $.post('/reg', field, function (data, status) {
                   console.log(data,status)
                    if (data.code == 200) {
                        layer.msg('注册成功！', { icon: 1, time: 1000 }, function () {
                           window.location.href = '/'
                        });
                    }
                }).error(function(xhr){
                    layer.msg(xhr.responseJSON.message, { icon: 2, time: 1000 });
                });

                return false; // 阻止默认 form 跳转
            });

            
        });
    </script>


</body>

</html>